<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<html>
<head>
    <title>编辑用户</title>
    <%@ include file="/include/openstyles.jsp" %>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery.validate.min.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/validate-methods.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery/jquery-form.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/input.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/lui.jiantingdiv.js"></script>
    <script type="text/javascript" src="${ctx}/assets/uc/js/common.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/datetimepicker/jquery.datetimepicker.js"></script>
    <link type="text/css" href="${ctx}/assets/admin/LingUI/datetimepicker/css/jquery.datetimepicker.css"/>
    <style>
        .help-block {
            margin: 0px !important;
        }
    </style>
</head>
<body>
<div class="row">
    <div class="col-xs-12">
        <div class="space-12"></div>
        <form class="form-horizontal" role="form" id="user_form">
            <div class="tabbable">
                <div class="profile-user-info profile-user-info-striped">
                    <div class="profile-info-row">
                        <div class="profile-info-name">用户编号：</div>
                        <div class="profile-info-value">
                            <input type="text" id="username" name="username"
                                   class="col-xs-10 col-sm-8 " placeholder="字母数字和下划线组成"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">用户名称：</div>
                        <div class="profile-info-value">
                            <input type="text" id="name" name="name"
                                   class="col-xs-10 col-sm-8" placeholder="汉字字母数字或下划线组成"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">性别：</div>
                        <div class="profile-info-value">
                            <select id="gender" name="gender"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">证件类型：</div>
                        <div class="profile-info-value">
                            <select name="certificateType" id="certificateType" title="">
                                <option value="-1">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">证件号：</div>
                        <div class="profile-info-value">
                            <input type="text" id="certificateNum" name="certificateNum"
                                   class="col-xs-10 col-sm-8" placeholder="请填写正确的证件号"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">单位：</div>
                        <div class="profile-info-value">
                            <input type="text" id="company" name="company"
                                   class="col-xs-10 col-sm-8" placeholder="请填写正确的单位名称"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">单位性质1：</div>
                        <div class="profile-info-value">
                            <select id="companyType1" name="companyType1"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">单位性质2：</div>
                        <div class="profile-info-value">
                            <select id="companyType2" name="companyType2"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">所属部门：</div>
                        <div class="profile-info-value">
                            <input type="text" id="department" name="department"
                                   class="col-xs-10 col-sm-8" placeholder="请填写正确的部门名称"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">职称：</div>
                        <div class="profile-info-value">
                            <input type="text" id="zhiCheng" name="zhiCheng"
                                   class="col-xs-10 col-sm-8" placeholder="请填写正确的职称"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">电子邮件：</div>
                        <div class="profile-info-value">
                            <input type="text" id="email" name="email"
                                   class="col-xs-10 col-sm-8" placeholder="电子邮件"/>
                        </div>
                    </div>

                  <%--  <div class="profile-info-row">
                        <div class="profile-info-name">出生日期：</div>
                        <div class="profile-info-value">
                            <input id="birthday" name="birthday" type="text"
                                   class="col-xs-10 col-sm-8">
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">固定电话：</div>
                        <div class="profile-info-value">
                            <input id="tel" name="tel" type="text"
                                   class="col-xs-10 col-sm-8" placeholder="固定电话">
                        </div>
                    </div>--%>
                    <div class="profile-info-row">
                        <div class="profile-info-name">移动电话：</div>
                        <div class="profile-info-value">
                            <input id="mobile" name="mobile" type="text"
                                   class="col-xs-10 col-sm-8" placeholder="移动电话">
                        </div>
                    </div>
                   <%-- <div class="profile-info-row">
                        <div class="profile-info-name">联系地址：</div>
                        <div class="profile-info-value">
                            <input id="address" name="address" type="text"
                                   class="col-xs-10 col-sm-8">
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">邮政编码：</div>
                        <div class="profile-info-value">
                            <input id="post" name="post" type="text"
                                   class="col-xs-10 col-sm-8">
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">用户类型：</div>
                        <div class="profile-info-value">
                            <select id="userType" name="userType"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>--%>
                    <div class="profile-info-row">
                        <div class="profile-info-name">是否启用：</div>
                        <div class="profile-info-value">
                            <select id="enabled" name="enabled"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">用户岗位：</div>
                        <div class="profile-info-value">
                            <select id="position" name="position"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">排序字段：</div>
                        <div class="profile-info-value">
                            <input type="text" id="order" name="order"
                                   class="col-xs-10 col-sm-8" placeholder="数字组成"/>
                        </div>
                    </div>
                   <%-- <div class="profile-info-row">
                        <div class="profile-info-name">毕业日期：</div>
                        <div class="profile-info-value">
                            <input id="graduateDate" name="graduateDate" type="text"
                                   class="col-xs-10 col-sm-8">
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">入职日期：</div>
                        <div class="profile-info-value">
                            <input id="employDate" name="employDate" type="text"
                                   class="col-xs-10 col-sm-8">
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">调休(小时)：</div>
                        <div class="profile-info-value">
                            <input type="text" id="overtimeAmount" name="overtimeAmount"
                                   class="col-xs-10 col-sm-8" placeholder="正整数"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">年休假(小时)：</div>
                        <div class="profile-info-value">
                            <input type="text" id="annualAmount" name="annualAmount"
                                   class="col-xs-10 col-sm-8" placeholder="正整数"/>
                        </div>
                    </div>--%>
                    <div class="profile-info-row">
                        <div class="profile-info-name">描述信息：</div>
                        <div class="profile-info-value">
							<textarea id="description" name="description"
                                      style="resize:none;padding-left:3px !important;"
                                      maxlength="200" rows="3" class="col-xs-10 col-sm-8"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>


<script type="text/javascript">

    function fillForm() {
        $('#username').val('${user.username}').attr('disabled', true);
        $('#name').val('${user.name}');
        $('#gender').val('${user.gender}');
        $('#certificateType').val('${user.certificateType}');//证件类型
        $('#email').val('${user.email}');
        $('#certificateNum').val('${user.certificateNum}');//证件号
        $('#company').val('${user.company}');//单位
        $('#mobile').val('${user.mobile}');
        $('#companyType1').val('${user.companyType1}');//单位性质1
        $('#companyType2').val('${user.companyType2}');//单位性质1
        $('#department').val('${user.department}');//所属部门
        $('#enabled').val('${user.enabled}');
        $('#position').val('${user.position}');
        $('#order').val('${user.order}');
        $('#zhiCheng').val('${user.zhiCheng}');
        /* $('#annualAmount').val('${user.annualAmount}');
        $('#graduateDate').val('${user.graduateDate}');
        $('#employDate').val('${user.employDate}');*/
    }

    $(function () {
        loadUserGenderItems();
        loadCertificateType();
        loadCompanyType1();
        loadCompanyType2();
        loadUserEnabledItems();
        loadUserPositions();
        jQuery('#birthday').datetimepicker({
            format: 'Y-m-d',
            timepicker: false,
            todayButton: true
        });
        jQuery('#graduateDate').datetimepicker({
            format: 'Y-m-d',
            timepicker: false,
            todayButton: true
        });
        jQuery('#employDate').datetimepicker({
            format: 'Y-m-d',
            timepicker: false,
            todayButton: true
        });
        fillForm();
    });

    //从数据字典中加载用户性别
    function loadUserGenderItems() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/USER_GENDER/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                })
                $("#gender").empty();
                $("#gender").append(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }

    //从数据字典中加载用户类型
    function loadUserTypeItems() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/USER_TYPE/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                })
                $("#userType").empty();
                $("#userType").append(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }

    //从数据字典中加载是否启用items
    function loadUserEnabledItems() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/COMMON_YESNO/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                })
                $("#enabled").empty();
                $("#enabled").append(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }

    //加载用户岗位
    function loadUserPositions() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/user/position?org=${orgId}',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.ID + "'>" + item.NAME + "</option>";
                })
                $("#position").empty();
                $("#position").append(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }
    //加载证件类型
    function loadCertificateType() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/Certificate_Type/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                });
                $("#certificateType").empty();
                $("#certificateType").html(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }

    //加载单位性质1类型
    function loadCompanyType1() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/Company_Type1/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                });
                $("#companyType1").empty();
                $("#companyType1").html(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }

    //加载单位性质2类型
    function loadCompanyType2() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/item/Company_Type2/sub',
            async: false,
            dataType: 'json',
            success: function (data) {
                var tempAjax = "";
                $.each(data, function (idx, item) {
                    tempAjax += "<option value='" + item.value + "'>" + item.name + "</option>";
                });
                $("#companyType2").empty();
                $("#companyType2").html(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                lui.fail('加载数据失败！');
            }
        });
    }
    var form = "user_form";
    var $form = $("#" + form);

    jQuery(function ($) {
        //账号验证
        jQuery.validator.addMethod("validateId", function (value, element) {
            var zipcode = new RegExp("^[A-Za-z0-9]{1}[A-Za-z0-9_]{0,19}$");
            return this.optional(element) || zipcode.test(value);
        }, "输入一个有效的账号，账号不能以下划线开头，长度小于20位！");

        //姓名验证
        jQuery.validator.addMethod("validateName", function (value, element) {
            var zipcode = new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]{1}[A-Za-z0-9\u4e00-\u9fa5_]{0,19}$");
            return this.optional(element) || zipcode.test(value);
        }, "输入一个有效的姓名，不能以下划线开头，长度最多20位！");

        //生日验证
        jQuery.validator.addMethod("validateDate", function (value, element) {
            var zipcode = /^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/;
            return this.optional(element) || zipcode.test(value);
        }, "输入有效的生日！");

        $form.validate({
            rules: {
                'name': {
                    required: true,
                    validateName: true
                },
                'order': {
                    required: true
                },
                'username': {
                    required: true,
                    validateId: true
                },
                'email': {
                    required: true,
                    email: true
                },
                'birthday': {
                    required: true
                },
                'overtimeAmount': {
                    required: true,
                    digits: true
                },
                'annualAmount': {
                    required: true,
                    digits: true
                }
            },

            messages: {
                'name': {
                    required: "名称不能为空"
                },
                'order': {
                    required: "排序字段不能为空"
                },
                'username': {
                    required: "编号不能为空"
                }/*,
                'overtimeAmount': {
                    required: "调休不能为空"
                },
                'annualAmount': {
                    required: "年休假不能为空"
                }*/
            }
        });

    });

    function formValid() {
        var retObj = new Object();
        var valid = $("#" + form).valid();
        if (valid) {
            $form.ajaxSubmit({
                url: "${ctx}/${frameworkPath}/user/edit/${user.username}",
                type: "post",
                async: false,
                dataType: 'json',
                success: function (message) {
                    if (message.success) {
                        retObj.text = "保存成功！";
                        retObj.state = true;
                    } else {
                        retObj.text = "保存失败！";
                        retObj.state = false;
                    }
                }
            });
        } else {
            return;
        }
        return retObj;
    }
</script>
</body>
</html>
